package org.geotools;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.ServiceInfo;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.filter.text.cql2.CQL;
import org.geotools.filter.text.cql2.CQLException;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ConPostgres {
    public static void main(String[] args) throws IOException, CQLException {
        Map<String, Object> params = new HashMap<>();
        params.put("dbtype", "postgis");
        params.put("host", "localhost");
        params.put("port", 5432);
        params.put("schema", "public");
        params.put("database", "Test");
        params.put("user", "postgres");
        params.put("passwd", "123456");

        DataStore dataStore = DataStoreFinder.getDataStore(params);
        System.out.println(dataStore);

        // exampleInfo start
        ServiceInfo info = dataStore.getInfo();
        System.out.println(info);

        // Human readable name and description
        String title = info.getTitle();
        String text = info.getDescription();

        // keywords (dublin core keywords like a web page)
        Set<String> keywords = info.getKeywords();

        // formal metadata
        URI publisher = info.getPublisher(); // authority publishing data
        URI schema = info.getSchema(); // used for data conforming to a standard
        URI source = info.getSource(); // location where information is published from
        SimpleFeatureSource a = dataStore.getFeatureSource("bou2_4p");
        Filter filter = CQL.toFilter("id=0");
        SimpleFeatureCollection b = a.getFeatures(filter);
        SimpleFeatureIterator itertor = b.features();
        while (itertor.hasNext())
        {
            //获取每一个要素
            SimpleFeature feature = itertor.next();
            System.out.println(feature.getAttributes());
//            System.out.println(feature.getAttribute("gid"));
        }
//        System.out.println(title);
//        System.out.println(text);
//        System.out.println(publisher);
//        System.out.println(schema);
//        System.out.println(source);
//        System.out.println(dataStore.getTypeNames());
    }
}
